const fs = require('fs');
const download = require('download');

// 不良女家庭教師
const items = [
  322893, 230424, 230951, 231568, 231972, 232557, 233661, 235404, 238062, 239775, 241277, 242653, 243925, 245094, 246111, 247141, 248328, 249725, 250535, 251857, 252818, 254250, 256373, 257605, 259035, 260468, 261781, 263315, 264794, 266067, 267474, 268746, 271247, 272491, 274111, 275729, 276983,
  278240, 279467, 280731, 282029, 283267, 285537, 286771, 288077, 289742, 291250, 292611, 293846, 295391, 297096, 298583, 300013, 301464, 303022, 304466, 305863, 319725, 321966, 324047, 325950, 330258, 332017,
];

const writeFolder = `dist/不良女家庭教師`;

async function run2(id, i) {
  const page = i + 1;
  for (let j = 1; j <= 1000; j++) {
    const pageFolder = `${writeFolder}/${page}`;
    const count = `00${j > 9 ? (j > 99 ? j : `0${j}`) : `00${j}`}`;
    const writePath = `${pageFolder}/${count}.jpg`;

    if (fs.existsSync(writePath)) {
      console.warn(`文件已存在：${writePath}`);
      continue;
    }

    const url = `https://cdn-msp.jmcomic.mobi/media/photos/${id}/${count}.jpg`;
    const buffer = await download(url);
    if (!buffer || buffer.length <= 0) {
      console.log(`下载失败：${writePath}, 大小为 0`);
      break;
    }
    try {
      if (!fs.existsSync(pageFolder)) {
        fs.mkdirSync(pageFolder);
      }
    } catch (err) {
      console.error(err.message);
    }
    fs.writeFileSync(writePath, buffer.toString('base64url'));
    console.log(`已经下载：${writePath}`);
  }
}

const startPage = 29;

async function run() {
  for (let i = startPage; i < items.length; i++) {
    const id = items[i];
    await run2(id, i);
  }
}

run();
